
package com.platform.modules.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.common.utils.JedisUtil;
import com.platform.common.utils.Query;
import com.platform.modules.platform.dao.PlatformColumnDao;
import com.platform.modules.platform.entity.PlatformColumnEntity;
import com.platform.modules.platform.service.PlatformColumnService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 首页底部菜单，个人中心栏目设置表Service实现类
 *
 * @author 
 * @date 2022-09-21 08:59:43
 */
@Service("platformColumnService")
public class PlatformColumnServiceImpl extends ServiceImpl<PlatformColumnDao, PlatformColumnEntity> implements PlatformColumnService {

    @Resource
    JedisUtil jedisUtil;

    @Override
    public List<PlatformColumnEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<PlatformColumnEntity> page = new Query<PlatformColumnEntity>(params).getPage();
        return page.setRecords(baseMapper.selectPlatformColumnPage(page, params));
    }

    @Override
    public boolean add(PlatformColumnEntity platformColumn) {
        Integer type = platformColumn.getType();
        jedisUtil.del(Constant.COLUMN + type);
        return this.save(platformColumn);
    }

    @Override
    public boolean update(PlatformColumnEntity platformColumn) {
        Integer type = platformColumn.getType();
        jedisUtil.del(Constant.COLUMN + type);
        return this.updateById(platformColumn);
    }

    @Override
    public boolean delete(String id) {
        PlatformColumnEntity columnEntity = this.getById(id);
        if(ObjectUtils.allNotNull(columnEntity)){
            Integer type = columnEntity.getType();
            jedisUtil.del(Constant.COLUMN + type);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        jedisUtil.del(Constant.COLUMN + 1);
        jedisUtil.del(Constant.COLUMN + 0);
        return this.removeByIds(Arrays.asList(ids));
    }


    @Override
    public List<PlatformColumnEntity> getList(Integer type) {
        List<PlatformColumnEntity> columnEntities = this.list(new LambdaQueryWrapper<PlatformColumnEntity>().eq(PlatformColumnEntity::getType, type).eq(PlatformColumnEntity::getParentId, "0")
                .eq(PlatformColumnEntity::getIsShow, 1).orderByDesc(PlatformColumnEntity::getSort));
        if (columnEntities != null && columnEntities.size() > 0) {
            for (PlatformColumnEntity columnEntity : columnEntities) {
                columnEntity.setColumnEntities(this.list(new LambdaQueryWrapper<PlatformColumnEntity>().eq(PlatformColumnEntity::getParentId, columnEntity.getId())
                        .eq(PlatformColumnEntity::getIsShow, 1).orderByDesc(PlatformColumnEntity::getSort)));
            }
        }
        jedisUtil.set(Constant.COLUMN + type, JSONObject.toJSON(columnEntities).toString(), 0);

        return columnEntities;
    }
}
